import redis
from ronglian_sms_sdk import SmsSDK
import random,json

class SmsCode():
    def __init__(self):
        accId='8a216da88185a9c001818a5f2af30106'
        accToken='52bfcd03e66949e0ad9952a3f0d871b2'
        appId='8a216da88185a9c001818a5f3398010d'
        self.sdk = SmsSDK(accId, accToken, appId)

    def send(self,phone,code):
        datas=(code,12)
        sdk=self.sdk.sendMessage('1', phone, datas)
        # 转换为json字符串
        rds_str = json.loads(sdk)
        # 存入redis中
        rds=redis.Redis(host='127.0.0.1')
        key='sms_%s'%phone
        rds.set(key,code,ex=12*6)


        if rds_str['statusCode']=='000000':
            return True
        return False

